package com.ssm.module.film.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ssm.common.persistence.PageBean;
import com.ssm.module.film.entity.Film;
import com.ssm.module.film.entity.FilmQueryVo;
import com.ssm.module.film.service.FilmService;

/**  
* <p>Title: FilmController</p>  
* <p>Description: 影片控制器</p>  
* @author jzhang  
* @date 2018年6月12日 下午3:17:42  
*/  
@Controller
@RequestMapping("/film")
public class FilmController {
	@Autowired
	private FilmService filmService;
	
	//影片列表
	@RequestMapping("/list")
	public String list(FilmQueryVo filmQueryVo,Model model) throws Exception {
		List<Film> filmList = filmService.findList(filmQueryVo);
		model.addAttribute("filmQueryVo", filmQueryVo);
		model.addAttribute("filmList", filmList);
		return "film/filmList";
	}
	
	//影片表单
	@RequestMapping("/form")
	public String form(Film film,Model model) throws Exception {
		model.addAttribute("film", filmService.selectFilmById(film));
		return "film/filmForm";
	}
	
	//影片保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(Film film) throws Exception {
		return filmService.saveFilm(film);
	}
	
	//影片删除
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String ids) throws Exception {
		return filmService.deleteFilm(ids);
	}
	
	//查看详情页面
	@RequestMapping("/show")
	public String show(Film film,Model model) throws Exception{
		//根据id查询影片信息
		Film viewFilm = filmService.selectFilmById(film);
		model.addAttribute("film", viewFilm);
		return "film/filmInfo";
	}
	
	//上传图片
	@RequestMapping(value="/filmPicUpload",method={RequestMethod.POST})
	@ResponseBody
	public String filmPicUpload(@RequestParam("file_upload") CommonsMultipartFile filmPic) throws Exception{
		System.out.println("图片上传");
		//获取的图片原始名称
		String originalFilename = filmPic.getOriginalFilename();
		
		//上传图片
		if(filmPic != null && originalFilename != null && originalFilename.length() > 0){
			//存储图片的物理路径
			String storePath = "D:\\image\\";
			//新的图片名称
			String newFilename = UUID.randomUUID() 
					+ originalFilename.substring(originalFilename.lastIndexOf("."));
			//新图片
			File newFile = new File(storePath + newFilename);
			//将内存中数据写入磁盘
			filmPic.transferTo(newFile);
			//上传成功将图片路径写到itemsCustom属性中
			return newFilename;
		}
		return null;
	}
	
	@RequestMapping("/getFilmData")
	public String getFilmData(Model model,FilmQueryVo filmQueryVo) throws Exception{
		PageBean<Film> page = filmService.queryFilmByCondition(filmQueryVo);
		model.addAttribute("page", page);
		return "film/filmListDataPage";
	}
}
